package com.hds.modules.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.hds.common.utils.CaptchaUtil;
import com.hds.modules.app.dao.SMSCaptchaDao;
import com.hds.modules.app.entity.SMSCaptcha;
import com.hds.modules.app.service.SMSCaptchaService;
import com.hds.modules.app.utils.DateUtils;
import com.hds.modules.app.utils.SmsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service("smsCaptchaService")
public class SMSCaptchaServiceImpl extends ServiceImpl<SMSCaptchaDao, SMSCaptcha> implements SMSCaptchaService {

	@Autowired
	private SMSCaptchaDao smsCaptchaDao;
	@Autowired
	private SmsUtils smsUtils;


	/** The Constant INVALID_TIME. */
	private final int INVALID_TIME = 30 * 60 * 1000;

	/**
	 * [手机验证码校验]
	 */
	@Override
	public Boolean verify(String mobile, String captcha) {
		SMSCaptcha smsCaptcha = smsCaptchaDao.findLatest(mobile, captcha);
		if(smsCaptcha == null) { //验证码错误
			return false;//验证码错误
		} else {
			if(DateUtils.timespace(smsCaptcha.getGenerateTime(), new Date()) < INVALID_TIME) { //有效期内30mins
				smsCaptcha.setStatus(0);
				smsCaptchaDao.updateById(smsCaptcha);
				return true;
			} else { //验证码失效
				return false;
			}
		}
	}

	/**
	 * [获取验证码]
	 */
	@Override
	public Map<String, Object> getSMSCaptcha(String mobile) {
		// 如果上次发送短信的时间距离当前时间不足60秒的, 则不发送短信
		HashMap<String, Object> result = null;
		Map<String, Object> responseMap = new HashMap<String, Object>();

		CCPRestSDK restApi = CaptchaUtil.getCCPRestSDK(smsUtils.getServerUrl(), smsUtils.getServerPort(), smsUtils.getMainAccount(), smsUtils.getMainToken(), smsUtils.getAppId()); //云通讯平台REST接口
		String captcha = CaptchaUtil.generateSmsCaptcha(); //验证码
		result = restApi.sendTemplateSMS(mobile, smsUtils.getTemplateId(), new String[]{captcha, "30"});
		String statusCode=result.get("statusCode").toString();
		if(SMSCaptcha.Code.SUCCESS.getCode().equals(statusCode)){ //发送成功
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			String createdDate = (String)((Map<String, String>)data.get("templateSMS")).get("dateCreated");
			String messageId = (String)((Map<String, String>)data.get("templateSMS")).get("smsMessageSid");
			SMSCaptcha smsCaptcha = new SMSCaptcha();
			try {
				Date generateTime = DateUtils.parseDate(createdDate, new String[]{"yyyyMMddHHmmss"});
				smsCaptcha.setId(UUID.randomUUID().toString());
				smsCaptcha.setGenerateTime(generateTime);
				smsCaptcha.setMobile(mobile);
				smsCaptcha.setCaptcha(captcha);
				smsCaptcha.setMessageId(messageId);
				smsCaptcha.setSendTime(new Date());
				smsCaptcha.setStatus(1);
				//发送验证码时将之前已发送的所有验证码设为失效,防止用户多次点击发送验证码未使用造成多条可用的验证码存在
				//smsCaptchaDao.setInvalid(mobile);
				smsCaptchaDao.insert(smsCaptcha);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			String statusMsg=result.get("statusMsg").toString();
			responseMap.put("statusMsg", statusMsg);
		}
		responseMap.put("smsStatusCode", statusCode);
		return responseMap;
	}




	/**
	 * 发送短信通知
	 *
	 * String chdsAppid = smsUtils.getAppId(); //红豆杉的appid
	 * String yabAppid = smsUtils.getAppId(); // 宜安保的appid
	 *
	 */
	@Override
	public Map<String, Object> getSMSNotice(String mobilePhone , String appid, String templateId, String... params) {
		HashMap<String, Object> result = null;
		Map<String, Object> responseMap = new HashMap<>();

        
		CCPRestSDK restApi = CaptchaUtil.getCCPRestSDK(smsUtils.getServerUrl(),
				smsUtils.getServerPort(), smsUtils.getMainAccount(), smsUtils.getMainToken(), appid); //云通讯平台REST接口

		result = restApi.sendTemplateSMS(mobilePhone, templateId, params);


		String statusCode=result.get("statusCode").toString();
		if(SMSCaptcha.Code.SUCCESS.getCode().equals(statusCode)){
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			String createdDate = (String)((Map<String, String>)data.get("templateSMS")).get("dateCreated");
			String messageId = (String)((Map<String, String>)data.get("templateSMS")).get("smsMessageSid");
		}else{
			String statusMsg=result.get("statusMsg").toString();
			responseMap.put("statusMsg", statusMsg);
		}
		responseMap.put("statusCode", statusCode);
		return responseMap;

	}


}
